﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AiDrawingWinform
{
    internal class ChatBox : RichTextBox
    {
        public ChatBox()
        {
            ReadOnly = true;
            BackColor = SystemColors.Window;
        }

        public enum ChatRole
        {
            User,
            System
        }

        public void AppendMessage(string message, ChatRole role)
        {
            void OutputString(string str, Color color, bool bold = false)
            {
                this.AppendText(str);
                this.SelectionStart -= str.Length;
                this.SelectionLength = str.Length;
                this.SelectionColor = color;
                if (bold)
                {
                    this.SelectionFont = new Font(this.Font, FontStyle.Bold);
                }
            }

            var color = role switch
            {
                ChatRole.User => Color.Blue,
                ChatRole.System => Color.Red,
                _ => this.SelectionColor
            };

            var name = role switch { ChatRole.User => "用户", ChatRole.System => "系统", _ => "" };

            OutputString($"【{DateTime.Now}】 ", Color.Gray, true);
            OutputString($"{name}：", Color.Black, true);
            OutputString(message, color);
            this.AppendText(Environment.NewLine);

            this.ScrollToCaret();
        }

    }
}
